<template>
  <div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item label="订单号"
          ><el-input
            size="medium"
            v-model="searchForm.orderNo"
            placeholder="请输入订单号"
          ></el-input
        ></el-form-item>
        <el-form-item label="外部订单号"
          ><el-input
            size="small"
            v-model="searchForm.outorderNo"
            placeholder="请输入订单号"
          ></el-input
        ></el-form-item>
        <el-form-item label="商品名称"
          ><el-input
            size="medium"
            v-model="searchForm.productName"
            placeholder="请输入商品名称"
          ></el-input
        ></el-form-item>
        <el-form-item label="商品编码"
          ><el-input
            size="medium"
            v-model="searchForm.productNo"
            placeholder="请输入商品编码"
          ></el-input
        ></el-form-item>
        <el-form-item label="商家名称"
          ><el-input
            size="medium"
            v-model="searchForm.supplierName"
            placeholder="请输入商家名称"
          ></el-input
        ></el-form-item>
        <el-form-item label="客户昵称"
          ><el-input
            size="medium"
            v-model="searchForm.userName"
            placeholder="请输入客户昵称"
          ></el-input
        ></el-form-item>
        <el-form-item label="客户手机号"
          ><el-input
            size="medium"
            v-model="searchForm.userMobile"
            placeholder="请输入客户手机号"
          ></el-input
        ></el-form-item>
        <el-form-item label="收货人姓名"
          ><el-input
            size="medium"
            v-model="searchForm.addressName"
            placeholder="请输入收货人姓名"
          ></el-input
        ></el-form-item>
        <el-form-item label="收货人手机号"
          ><el-input
            size="medium"
            v-model="searchForm.addressPhone"
            placeholder="请输入收货人手机号"
          ></el-input
        ></el-form-item>
        <el-form-item label="支付订单号"
          ><el-input
            size="medium"
            v-model="searchForm.tradeNo"
            placeholder="请输入支付订单号"
          ></el-input
        ></el-form-item>
        <el-form-item label="配送方式">
          <el-select
            style="width: 160px"
            size="medium"
            v-model="searchForm.style_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="(item, index) in exStyle"
              :key="index"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单来源">
          <el-select
            style="width: 160px"
            size="medium"
            v-model="searchForm.orderSources"
            placeholder="请选择"
            filterable
            clearable
            multiple
          >
            <el-option
              v-for="(item, index) in orderSourcesList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式">
          <el-select
            style="width: 160px"
            size="medium"
            v-model="searchForm.payTypes"
            placeholder="请选择"
            filterable
            clearable
            multiple
          >
            <el-option
              v-for="(item, index) in payTypesList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自提门店">
          <el-select
            style="width: 160px"
            size="medium"
            v-model="searchForm.extractStoreIds"
            placeholder="请选择"
            filterable
            clearable
            multiple
          >
            <el-option
              v-for="(item, index) in storeList"
              :key="index"
              :label="item.storeName"
              :value="item.storeId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="贸易方式">
          <el-select
            style="width: 160px"
            size="medium"
            v-model="searchForm.tradeTypeIds"
            placeholder="请选择"
            filterable
            clearable
            multiple
          >
            <el-option
              v-for="(item, index) in tradeTypeList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标记">
          <el-select
            style="width: 160px"
            size="medium"
            v-model="searchForm.signalType"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="(item, index) in signalTypeList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营销类型">
          <el-select
            style="width: 160px"
            size="medium"
            v-model="searchForm.marketingTypes"
            placeholder="请选择"
            filterable
            clearable
            multiple
          >
            <el-option
              v-for="(item, index) in marketingTypesList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库">
          <el-select
            style="width: 160px"
            size="medium"
            v-model="searchForm.wares"
            placeholder="请选择"
            filterable
            clearable
            multiple
          >
            <el-option
              v-for="(item, index) in waresList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型">
          <el-select
            style="width: 160px"
            size="medium"
            v-model="searchForm.pushWareStatus"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="(item, index) in pushWareStatusList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起始时间">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              size="small"
              v-model="searchForm.createTime"
              type="daterange"
              value-format="YYYY-MM-DD"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="完成时间">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              size="small"
              v-model="searchForm.finishTime"
              type="daterange"
              value-format="YYYY-MM-DD"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item
          ><el-button
            size="small"
            type="primary"
            icon="Search"
            @click="onSubmit"
            >查询</el-button
          ></el-form-item
        >
        <el-form-item
          ><el-button
            size="small"
            type="success"
            :loading="exportLoading"
            @click="onExport"
            v-auth="'/order/operate/export'"
            >导出</el-button
          ></el-form-item
        >
        <el-form-item
          ><el-button
            size="small"
            type="warning"
            @click="onExportDownload"
            v-auth="'/order/operate/export'"
            >导出下载</el-button
          ></el-form-item
        >
      </el-form>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-tabs v-model="activeName" @tab-change="handleClick">
          <el-tab-pane label="全部订单" name="all"></el-tab-pane>
          <el-tab-pane
            :label="'未付款(' + order_count.payment + ')'"
            name="payment"
          ></el-tab-pane>
          <el-tab-pane
            :label="'待发货(' + order_count.delivery + ')'"
            name="delivery"
          ></el-tab-pane>
          <el-tab-pane
            :label="'待收货(' + order_count.received + ')'"
            name="received"
          ></el-tab-pane>
          <el-tab-pane label="待评价" name="comment"></el-tab-pane>
          <el-tab-pane label="已完成" name="six"></el-tab-pane>
          <el-tab-pane label="已取消" name="cancel"></el-tab-pane>
          <el-tab-pane label="取消申请中" name="cancelApply"></el-tab-pane>
        </el-tabs>
        <el-table
          size="small"
          :data="tableData.data"
          :span-method="arraySpanMethod"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column label="订单信息" width="400">
            <template #default="scope">
              <div class="order-top" v-if="scope.row.isTopRow">
                <div class="order-code">
                  <span
                    v-if="scope.row.orderSourceText"
                    class="state-text"
                    :class="{ 'state-text-red': scope.row.orderSource != 10 }"
                    >{{ scope.row.orderSourceText }}</span
                  >
                  <span class="state-text">{{
                    scope.row.crossBorder == 1 ? "跨境" : "非跨境"
                  }}</span>
                  <span class="c_main">订单号：{{ scope.row.orderNo }}</span>
                  <span class="pl16">下单时间：{{ scope.row.createTime }}</span>
                  <!--是否取消申请中-->
                  <span class="pl16" v-if="scope.row.orderStatus == 21"
                    ><el-tag effect="dark" size="mini">取消申请中</el-tag></span
                  >
                  <span v-if="scope.row.outorderNo" class="pl16"
                    >外部订单号：{{ scope.row.outorderNo }}</span
                  >
                </div>
                <div
                  style="cursor: pointer"
                  v-if="scope.row.signalType"
                  @click="onRemark(scope.row)"
                >
                  <img
                    v-if="scope.row.signalType == '1'"
                    style="width: 24px"
                    src="/static/imgs/sign-type-1.png"
                  />
                  <img
                    v-else-if="scope.row.signalType == '2'"
                    style="width: 24px"
                    src="/static/imgs/sign-type-2.png"
                  />
                  <img
                    v-else-if="scope.row.signalType == '3'"
                    style="width: 24px"
                    src="/static/imgs/sign-type-3.png"
                  />
                  <img
                    v-else-if="scope.row.signalType == '4'"
                    style="width: 24px"
                    src="/static/imgs/sign-type-4.png"
                  />
                  <img
                    v-else-if="scope.row.signalType == '5'"
                    style="width: 24px"
                    src="/static/imgs/sign-type-5.png"
                  />
                </div>
              </div>
              <template v-else>
                <div
                  class="product-info"
                  v-for="(item, index) in scope.row.productList"
                  :key="index"
                >
                  <div class="pic">
                    <img
                      v-img-url="
                        item.imagePath +
                        '?x-oss-process=image/resize,w_120,Q_80'
                      "
                      alt
                    />
                  </div>
                  <div class="info">
                    <div class="name gray3 product-name">
                      <span>{{ item.productName }}</span>
                    </div>
                    <div class="gray9" v-if="item.productAttr">
                      {{ item.productAttr }}
                    </div>
                    <div class="orange" v-if="item.refund">
                      {{ item.refund.type.text }}-{{ item.refund.status.text }}
                    </div>
                  </div>
                  <div class="d-c-c d-c">
                    <div class="orange">￥ {{ item.productPrice }}</div>
                    <div class="gray3">x{{ item.totalNum }}</div>
                  </div>
                </div>
              </template>
            </template>
          </el-table-column>
          <el-table-column
            prop="pay_price"
            label="实付款"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              <!-- <div v-if="!scope.row.isTopRow">
              <div class="orange">{{ scope.row.payPrice }}</div>
              <p class="gray9">(含运费：{{ scope.row.expressPrice }})</p>
              </div> -->
              <div v-if="!row.isTopRow">
                <template v-if="row.orderSource == 80">
                  <p class="orange">定金：{{ row.money }}</p>
                  <p class="orange">尾款：{{ row.balancePayment }}</p>
                </template>
                <div class="orange" v-else>
                  {{ row.payPrice }}
                </div>
                <p class="gray9">(含运费：{{ row.expressPrice }})</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="" label="买家">
            <template #default="scope">
              <div v-if="!scope.row.isTopRow">
                <div>{{ scope.row.nickName }}</div>
                <div class="gray9">ID：({{ scope.row.userId }})</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="supplierName"
            label="商户名称"
          ></el-table-column>
          <el-table-column label="交易状态">
            <template #default="scope">
              <div v-if="!scope.row.isTopRow">
                {{ scope.row.orderStatusText }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="pay_type.text" label="支付方式">
            <template #default="scope">
              <div v-if="!scope.row.isTopRow">
                <span class="gray9">{{ scope.row.payTypeText }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="delivery_type.text" label="配送方式">
            <template #default="scope">
              <div v-if="!scope.row.isTopRow">
                <span class="green">{{ scope.row.deliveryTypeText }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="评价" width="60">
            <template #default="scope">
              <div v-if="!scope.row.isTopRow">
                <span v-if="scope.row.isComment == 0">未评价</span>
                <span v-else="">已评价</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <template #default="scope">
              <div v-if="!scope.row.isTopRow">
                <el-button @click="onRemark(scope.row)" type="text" size="small"
                  >标记</el-button
                >
                <el-button
                  @click="addClick(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/order/order/detail'"
                  >详情</el-button
                >
                <el-button
                  v-if="
                    scope.row.deliveryType == 10 &&
                    scope.row.deliveryStatus == 20
                  "
                  @click="getLogistics(scope.row)"
                  link
                  type="primary"
                  size="small"
                  v-auth="'/order/order/express'"
                  >查看物流
                </el-button>
                <el-button
                  v-if="
                    scope.row.payStatus == 20 &&
                    +scope.row.refundMoney < +scope.row.payPrice &&
                    scope.row.orderSource != 100
                  "
                  @click="onRefund(scope.row)"
                  type="text"
                  size="small"
                  >退款</el-button
                >
                <el-button
                  v-if="
                    scope.row.orderStatus == 10 &&
                    scope.row.deliveryStatus != 20
                  "
                  @click="onCancel(scope.row)"
                  type="text"
                  size="small"
                  >取消</el-button
                >
                <!--              <el-button
                v-if="scope.row.payStatus == 20 && scope.row.deliveryStatus == 10 && scope.row.orderStatus != 20 && scope.row.orderStatus != 21 && scope.row.deliveryType == 10"
                @click="addClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/order/order/delivery'"
              >
                发货
              </el-button>

              <el-button
                v-if="scope.row.payStatus == 20 && scope.row.deliveryStatus == 10 && scope.row.orderStatus != 20 && scope.row.orderStatus != 21 && scope.row.deliveryType == 20"
                @click="addClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/order/operate/extract'"
              >
                核销
              </el-button>
              <el-button
                v-if="scope.row.payStatus == 20 && scope.row.deliveryStatus == 10 && scope.row.orderStatus != 20 && scope.row.orderStatus != 21 && scope.row.deliveryType == 30"
                @click="addClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/order/order/delivery'"
              >
                发货
              </el-button>
              <el-button
                v-if="scope.row.payStatus == 20 && scope.row.deliveryStatus == 10 && scope.row.orderStatus == 10"
                @click="cancelClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/order/operate/confirmCancel'"
              >
                取消
              </el-button>
              <el-button v-if="scope.row.orderStatus == 21" @click="addClick(scope.row)" type="text" size="small" v-auth="'/order/operate/confirmCancel'">审核</el-button> -->
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
    <Logistics
      v-if="isLogistics"
      :logisticsData="logisticsData"
      :isLogistics="isLogistics"
      @closeDialog="closeLogistics"
    ></Logistics>
    <el-dialog title="批量发货说明" v-model="dialogBatchDelivery" width="30%">
      <div>1、先按未发货条件筛选后，导出订单</div>
      <div>
        2、在导出excel中填写物流公司、物流单号，然后点批量发货导入填好的excel文件
      </div>
      <div>
        3、请检查物流公司是否已经添加到系统，
        <el-link type="primary" :underline="false" @click="gotoExpress()"
          >查看</el-link
        >
      </div>
      <div style="padding-top: 20px; padding-bottom: 20px; text-align: center">
        <el-upload
          class="avatar-uploader"
          ref="upload"
          action="string"
          accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          :before-upload="onBeforeUploadImage"
          :http-request="UploadImage"
          :show-file-list="false"
        >
          <el-button size="small" icon="Upload" type="primary"
            >上传发货表格</el-button
          >
        </el-upload>
      </div>
    </el-dialog>
    <el-dialog title="标记" v-model="dialogRemark" width="30%">
      <el-form size="small" :model="remarkParams">
        <el-form-item label="标记">
          <el-radio-group size="small" v-model="remarkParams.signalType">
            <el-radio label="1"
              ><img style="width: 24px" src="/static/imgs/sign-type-1.png"
            /></el-radio>
            <el-radio label="2"
              ><img style="width: 24px" src="/static/imgs/sign-type-2.png"
            /></el-radio>
            <el-radio label="3"
              ><img style="width: 24px" src="/static/imgs/sign-type-3.png"
            /></el-radio>
            <el-radio label="4"
              ><img style="width: 24px" src="/static/imgs/sign-type-4.png"
            /></el-radio>
            <el-radio label="5"
              ><img style="width: 24px" src="/static/imgs/sign-type-5.png"
            /></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="remarkParams.remake"
            style="width: 100%"
            type="textarea"
            placeholder="请输入备注"
            :rows="4"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button size="small" @click="dialogRemark = false"
            >取 消</el-button
          >
          <el-button size="small" type="primary" @click="addRemark"
            >确 定</el-button
          >
        </div>
      </template>
    </el-dialog>
    <!--处理-->
    <Cancel
      v-if="open_edit"
      :open_edit="open_edit"
      :order_no="order_no"
      @closeDialog="closeDialogFunc($event, 'edit')"
    ></Cancel>
    <el-dialog
      title="退款"
      append-to-body
      v-model="refundDialogVisible"
      width="500px"
    >
      <el-form
        ref="refundForm"
        size="small"
        label-width="90px"
        :model="refundParams"
      >
        <el-form-item
          label="退款金额："
          prop="refundMoney"
          :rules="[{ required: true, message: '请输入退款金额' }]"
        >
          <el-input-number
            v-model="refundParams.refundMoney"
            :precision="2"
            :step="0.01"
            step-strictly
            :min="0.01"
            :max="canRefundMoney"
            :controls="false"
          />
          <span style="color: red">（可退金额：{{ canRefundMoney }}）</span>
        </el-form-item>
        <el-form-item
          label="退款原因："
          prop="refundReason"
          :rules="[{ required: true, message: '请输入退款原因' }]"
        >
          <el-input
            type="textarea"
            :rows="3"
            v-model="refundParams.refundReason"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="refundDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onRefundConfirm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="导出下载"
      append-to-body
      v-model="exportDialogVisible"
      width="900px"
    >
      <el-table :data="exportList" border style="width: 100%">
        <el-table-column prop="id" label="订单导出id" width="100px">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column prop="status" label="状态"> </el-table-column>
        <el-table-column prop="errorMsg" label="失败原因"> </el-table-column>
        <el-table-column label="操作" width="100px">
          <template #default="scope">
            <el-button
              v-if="scope.row.status === '导出成功！'"
              type="text"
              size="small"
              @click="exportDownload(scope.row)"
              >下载</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog
      title="取消订单"
      append-to-body
      v-model="cancelDialogVisible"
      width="500px"
    >
      <el-form
        ref="cancelForm"
        size="small"
        label-width="90px"
        :model="cancelParams"
      >
        <el-form-item
          label="取消原因："
          prop="cancelReason"
          :rules="[{ required: true, message: '请选择取消原因' }]"
        >
          <el-select
            size="medium"
            v-model="cancelParams.cancelReason"
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in cancelReasonList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onCancelConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import OrderApi from "@/api/order.js";
import PorductApi from "@/api/product.js";
import StoreApi from "@/api/store.js";
import qs from "qs";
import Cancel from "./dialog/cancel.vue";
import { useUserStore } from "@/store";
import Logistics from "@/components/logistics/viewLogistics.vue";
const { token } = useUserStore();

export default {
  components: {
    Cancel,
    Logistics,
  },
  data() {
    return {
      token,
      /*切换菜单*/
      activeName: "all",
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      searchForm: {
        orderNo: "",
        outorderNo: "",
        style_id: "",
        store_id: "",
        createTime: "",
        finishTime: "",
        orderSources: [],
        payTypes: [],
        extractStoreIds: [],
        tradeTypeIds: [],
        marketingTypes: [],
        wares: [],
        pushWareStatus: "",
        productName: "",
        productNo: "",
        supplierName: "",
        userName: "",
        userMobile: "",
        addressName: "",
        addressPhone: "",
        tradeNo: "",
        signalType: "",
      },
      /*配送方式*/
      exStyle: [],
      orderSourcesList: [
        {
          label: "普通",
          value: 10,
        },
        {
          label: "积分",
          value: 20,
        },
        {
          label: "拼团",
          value: 30,
        },
        {
          label: "砍价",
          value: 40,
        },
        {
          label: "秒杀",
          value: 50,
        },
        {
          label: "礼包购",
          value: 60,
        },
        {
          label: "银豹",
          value: 100,
        },
      ],
      payTypesList: [
        {
          label: "余额支付",
          value: 10,
        },
        {
          label: "微信支付",
          value: 20,
        },
      ],
      signalTypeList: [
        {
          label: "黄",
          value: "1",
        },
        {
          label: "绿",
          value: "2",
        },
        {
          label: "蓝",
          value: "3",
        },
        {
          label: "黑",
          value: "4",
        },
        {
          label: "红",
          value: "5",
        },
      ],
      marketingTypesList: [
        {
          label: "优惠券",
          value: 1,
        },
        {
          label: "积分",
          value: 2,
        },
      ],
      waresList: [
        {
          label: "门店",
          value: 1,
        },
        {
          label: "公共仓",
          value: 2,
        },
        {
          label: "唯品会仓",
          value: 3,
        },
        {
          label: "行云仓",
          value: 4,
        },
      ],
      pushWareStatusList: [
        {
          label: "未推送",
          value: 0,
        },
        {
          label: "推送成功",
          value: 1,
        },
        {
          label: "推送失败",
          value: 2,
        },
      ],
      tradeTypeList: [],
      /*门店列表*/
      storeList: [],
      /*时间*/
      create_time: "",
      /*统计*/
      order_count: {
        payment: 0,
        delivery: 0,
        received: 0,
      },
      dialogBatchDelivery: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      logisticsData: {},
      isLogistics: false,
      /*当前编辑的对象*/
      order_no: "",
      dialogRemark: false,
      remarkParams: {
        orderId: "",
        remake: "",
        signalType: "",
      },
      refundDialogVisible: false,
      refundParams: {
        refundMoney: undefined,
        refundReason: undefined,
        orderId: undefined,
      },
      canRefundMoney: 0,
      exportDialogVisible: false,
      exportList: [],
      exportLoading: false,
      cancelParams: {
        orderId: "",
        cancelReason: "",
      },
      cancelDialogVisible: false,
      cancelReasonList: [
        {
          label: "收货人信息有误",
          value: 10,
        },
        {
          label: "商品数量或款式需调整",
          value: 20,
        },
        {
          label: "有更优惠的购买方案",
          value: 30,
        },
        {
          label: "商品缺货",
          value: 40,
        },
        {
          label: "我不想买了",
          value: 50,
        },
        {
          label: "其他原因",
          value: 60,
        },
      ],
    };
  },
  created() {
    /*获取列表*/
    this.getData();
    this.tradeTypeListInit();
  },
  methods: {
    onCancel(row) {
      this.cancelParams.orderId = row.orderId;
      this.cancelParams.cancelReason = "";
      this.cancelDialogVisible = true;
    },
    onCancelConfirm() {
      this.$refs.cancelForm.validate((valid) => {
        if (valid) {
          OrderApi.orderOperateCancel(this.cancelParams).then((res) => {
            ElMessage({
              message: "取消成功",
              type: "success",
            });
            this.getData();
            this.cancelDialogVisible = false;
          });
        }
      });
    },
    addRemark() {
      OrderApi.updateRemake(this.remarkParams).then((res) => {
        ElMessage({
          message: "保存成功",
          type: "success",
        });
        this.getData();
        this.dialogRemark = false;
      });
    },
    onRemark(item) {
      this.remarkParams.orderId = item.orderId;
      this.remarkParams.remake = item.remake;
      this.remarkParams.signalType = item.signalType;
      this.dialogRemark = true;
    },
    tradeTypeListInit() {
      PorductApi.tradeTypeIndex().then((res) => {
        this.tradeTypeList = res.data.records;
      });
      StoreApi.shoplist(
        {
          pageIndex: 1,
          pageSize: 99999,
        },
        true
      ).then((res) => {
        this.storeList = res.data.records;
      });
    },
    getLogistics(row) {
      let self = this;
      let Params = {
        orderId: row.orderId,
        expressId: row.expressId,
        expressNo: row.expressNo,
      };
      self.loading = true;
      OrderApi.orderExpress(Params, true)
        .then((res) => {
          self.loading = false;
          self.logisticsData = res.data.express.data;
          console.log(self.logisticsData);
          self.openLogistics();
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    openLogistics() {
      this.isLogistics = true;
      console.log(this.isLogistics);
    },
    onRefund(row) {
      this.refundParams.orderId = row.orderId;
      this.refundParams.refundMoney = undefined;
      this.refundParams.refundReason = undefined;
      this.canRefundMoney = +row.payPrice - +row.refundMoney;
      this.refundDialogVisible = true;
    },
    onRefundConfirm() {
      const self = this;
      self.$refs.refundForm.validate((valid) => {
        if (valid) {
          OrderApi.refundOrder(self.refundParams, true)
            .then((res) => {
              ElMessage({
                message: "操作成功",
                type: "success",
              });
              self.getData();
              self.refundDialogVisible = false;
            })
            .catch((error) => {
              self.loading = false;
            });
        }
      });
    },
    closeLogistics() {
      this.isLogistics = false;
    },
    /*跨多列*/
    arraySpanMethod(row) {
      if (row.rowIndex % 2 == 0) {
        if (row.columnIndex === 0) {
          return [1, 8];
        }
      }
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.getData();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getData();
    },

    /*切换菜单*/
    handleClick(tab, event) {
      let self = this;
      self.curPage = 1;
      self.getData();
    },

    onExportDownload() {
      this.exportDialogVisible = true;
      OrderApi.exportRecord().then((res) => {
        this.exportList = res.data.list;
      });
    },

    /*获取列表*/
    getData() {
      let self = this;
      let search = this.searchForm;
      let Params = {
        ...search,
      };
      Params.dataType = self.activeName;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      if (search.createTime && search.createTime.length > 1) {
        Params.startDate = search.createTime[0];
        Params.endDate = search.createTime[1];
      }
      if (search.finishTime && search.finishTime.length > 1) {
        Params.finishStartDate = search.finishTime[0];
        Params.finishEndDate = search.finishTime[1];
      }
      Params.orderNo = search.orderNo;
      Params.deliveryType = search.style_id;
      self.loading = true;
      OrderApi.orderlist(Params, true)
        .then((res) => {
          let list = [];
          for (let i = 0; i < res.data.orderList.records.length; i++) {
            let item = res.data.orderList.records[i];
            let topitem = {
              orderNo: item.orderNo,
              outorderNo: item.outorderNo,
              createTime: item.createTime,
              orderSource: item.orderSource,
              orderSourceText: item.orderSourceText,
              orderStatus: item.orderStatus,
              isTopRow: true,
              orderId: item.orderId,
              remake: item.remake,
              signalType: item.signalType,
              crossBorder: item.crossBorder,
            };
            list.push(topitem);
            list.push(item);
          }
          self.tableData.data = list;

          self.totalDataNumber = res.data.orderList.total;
          self.exStyle = res.data.deliveryList;
          self.order_count = res.data.orderCount;
          self.loading = false;
        })
        .catch((error) => {});
    },
    /*打开取消*/
    cancelClick(item) {
      this.order_no = item.orderNo;
      this.open_edit = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
    },
    /*打开添加*/
    addClick(row) {
      window.open(`/shop/#/order/order/Detail?orderId=${row.orderId}`);
    },

    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.tableData = [];
      this.getData();
    },
    async onExport() {
      let self = this;
      let search = this.searchForm;
      let Params = {
        ...search,
      };
      Params.dataType = self.activeName;
      Params.pageIndex = 1;
      Params.pageSize = 999999;
      if (search.createTime && search.createTime.length > 1) {
        Params.startDate = search.createTime[0];
        Params.endDate = search.createTime[1];
      }
      if (search.finishTime && search.finishTime.length > 1) {
        Params.finishStartDate = search.finishTime[0];
        Params.finishEndDate = search.finishTime[1];
      }
      Params.orderNo = search.orderNo;
      Params.deliveryType = search.style_id;
      this.exportLoading = true;
      OrderApi.batchExport(Params, true).then((res) => {
        ElMessage.success("导出中，请耐心等待");
        this.exportLoading = false;
      });
    },
    exportDownload(row) {
      window.open(row.url);
    },
    // 显示批量发货说明
    showBatchDelivery() {
      this.dialogBatchDelivery = true;
    },
    /*跳转物流公司*/
    gotoExpress() {
      let self = this;
      self.$router.push("/setting/express/index");
    },

    /*选择图片之前*/
    onBeforeUploadImage(file) {
      var fileType = file.name.substring(file.name.lastIndexOf(".") + 1);
      const isEXCEL = fileType === "xlsx";
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isEXCEL) {
        ElMessage.error("上传文件只能是excel格式!");
      }
      if (!isLt10M) {
        ElMessage.error("上传文件大小不能超过 10MB!");
      }
      return isEXCEL && isLt10M;
    },

    /*上传图片*/
    UploadImage(param) {
      let self = this;
      const loading = ElLoading.service({
        lock: true,
        text: "正在处理,请等待",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      const formData = new FormData();
      formData.append("iFile", param.file);
      OrderApi.batchDelivery(formData)
        .then((response) => {
          loading.close();
          self.dialogBatchDelivery = false;
          ElMessage({
            message: response.msg,
            type: "warning",
          });
        })
        .catch((response) => {
          loading.close();
          ElMessage({
            message: "本次处理失败",
            type: "warning",
          });
          param.onError();
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.product-info {
  padding: 10px 0;
  border-top: 1px solid #eeeeee;
}

.order-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.order-code .state-text {
  padding: 2px 4px;
  border-radius: 4px;
  background: #808080;
  color: #ffffff;
  margin-right: 6px;
}

.order-code .state-text-red {
  background: red;
}

.table-wrap .product-info:first-of-type {
  border-top: none;
}

.table-wrap .el-table__body tbody .el-table__row:nth-child(odd) {
  background: #f5f7fa;
}
</style>
